/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package configure;

import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author littlechild
 */
public class ConfigureInfo {
    final static String configureFile="iWallet.ini";
    private static Properties p=null;
    //reminder
    public final static String ReminderPort="ReminderPort";
    public final static String Reminders="Reminders";
    public final static String ReminderTimeval="ReminderTimeval";
    //DataSourceClassName
    public final static String DataSourceClassName="DataSourceClassName";
    public final static String ModelFactoryClassName="ModelFactoryClassName";
    public final static String Maxid="maxid";
    public final static String InfoURL="InfoURL";
    public final static String webRefreshingTime="webRefreshingTime(ms)";
    public final static boolean isDebug=true;
    
    private ConfigureInfo() {
    }
    public static String getProperty(String key) {
         if(p==null) {
             FileInputStream fin=null;
            try {
                fin = new FileInputStream(configureFile);
                p=new Properties();
                p.load(fin);
                fin.close();
                    //p=getDefaultProperties();
            } catch (IOException ex) {
                FileOutputStream out = null;
                try {
                    p = getDefaultProperties();
                    out = new FileOutputStream(configureFile);
                    p.store(out, null);
                } catch (IOException ex1) {
                    Logger.getLogger(ConfigureInfo.class.getName()).log(Level.SEVERE, null, ex1);
                } finally {
                    try {
                        out.close();
                    } catch (IOException ex1) {
                        Logger.getLogger(ConfigureInfo.class.getName()).log(Level.SEVERE, null, ex1);
                    }
                }
            }
         }
         if(p==null)
             return null;
         return p.getProperty(key);
    }
    private static Properties getDefaultProperties() {
        Properties c=new Properties();
        c.setProperty(ConfigureInfo.ReminderPort, "3102");
        //c.setProperty(ConfigureInfo.ReminderBPort, "3103");
        c.setProperty(ConfigureInfo.Reminders, "Reminders.dat");
        c.setProperty(ConfigureInfo.ReminderTimeval, new Integer(2*3600*1000).toString());
        c.setProperty(ConfigureInfo.DataSourceClassName, "dataaccess.DataSource");
        c.setProperty(ConfigureInfo.ModelFactoryClassName, "model.ModelFactory");
        c.setProperty(ConfigureInfo.Maxid, "0");
        c.setProperty(ConfigureInfo.InfoURL, "http://127.0.0.1");
        c.setProperty(ConfigureInfo.webRefreshingTime, "3000");
        System.out.println(c.getProperty(ConfigureInfo.ReminderTimeval));
        return c;
    }
    private static boolean setProperties(String key, String value) {
        p.setProperty(key,value);
        FileOutputStream out = null;
        try {
            p = getDefaultProperties();
            out = new FileOutputStream(configureFile);
            p.store(out, null);
            return true;
        } catch (IOException ex1) {
            Logger.getLogger(ConfigureInfo.class.getName()).log(Level.SEVERE, null, ex1);
        } finally {
            try {
                out.close();
            } catch (IOException ex1) {
                Logger.getLogger(ConfigureInfo.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return false;
    }
}
